package com.cskaoyan.controller.wx.ddp;

import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.MarketCouponInfo;
import com.cskaoyan.module.bean.param.BaseParam;
import com.cskaoyan.module.bo.CouponCreateParamBo;
import com.cskaoyan.module.bo.CouponListUserParamBo;
import com.cskaoyan.module.ddp.bo.CouponMyListParamsBo;
import com.cskaoyan.module.ddp.vo.CouponListVo;
import com.cskaoyan.module.ddp.vo.CouponVo;
import com.cskaoyan.module.pojo.MarketCoupon;
import com.cskaoyan.module.pojo.MarketCouponUser;
import com.cskaoyan.module.pojo.MarketUser;
import com.cskaoyan.module.vo.MarketCouponVo;
import com.cskaoyan.service.PopularizeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * PopularizeController
 *
 * @param
 * @return
 * @2022/4/6
 **/
@RestController
@RequestMapping("wx/coupon")
public class CouponVXController {


    @Autowired
    @Qualifier("popularizeServiceImpl")
    PopularizeService popularizeService;
    @RequestMapping("mylist")
    public BaseRespVo<CouponVo> mylist(CouponMyListParamsBo params) {
        CouponVo marketCouponVo=null;
        BaseRespVo ok=null;
        marketCouponVo=popularizeService.mylist(params);
        System.out.println("marketCouponVo = " + marketCouponVo);
        if (marketCouponVo != null) {
            return
                    BaseRespVo.ok(marketCouponVo);
        } else {
            return BaseRespVo.invalidData("显示个人优惠卷出错！");
        }
    }

    @RequestMapping("receive")
    public BaseRespVo<MarketCouponUser> receive(@RequestBody Map couponId) {
        Subject subject = SecurityUtils.getSubject();
        System.out.println("subject = " + subject.getPrincipals());
        if (subject.getPrincipals() == null) {
            return BaseRespVo.invalidData("请登录!");
        }
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();

        MarketCouponUser marketCoupon = null;
        BaseRespVo ok = null;
        Integer couponId1 = (Integer) couponId.get("couponId");
        marketCoupon = popularizeService.receive(couponId1);
        if (marketCoupon != null) {
            return BaseRespVo.ok(marketCoupon);
        } else {
            return BaseRespVo.invalidData("已领取!");
        }
//         errmsg: "请登录"
//	       errno: 501
    }
    @RequestMapping("exchange")
    public BaseRespVo<MarketCoupon> exchange(@RequestBody String code) {
        MarketCoupon marketCoupon=null;
        System.out.println("create params = " + code);
//{"code":"ddp"}
        int index = code.indexOf(":");
        int end = code.indexOf("}");
        String substring = code.substring(index + 2, end-1);
        marketCoupon = popularizeService.exchange(substring);
        System.out.println("兑换优惠卷 code = " + substring);
        System.out.println("兑换优惠卷 result = " + marketCoupon);
        if (marketCoupon == null) {
            return BaseRespVo.invalidData("兑换优惠卷出错！已兑换或者兑换码输入错误！");
        } else {
            return BaseRespVo.ok(marketCoupon);
        }
    }
    @RequestMapping("list")
//    page=1&limit=10
//    首页进去 显示优惠卷
    public BaseRespVo<CouponListVo> list(CouponMyListParamsBo params) {
        CouponListVo marketCoupon=null;
        System.out.println("list baseParam = " + params);


        marketCoupon=popularizeService.listAllCoupons(params);
        System.out.println("首页进去 显示优惠卷 result = " + marketCoupon);
        if (marketCoupon != null) {
            return BaseRespVo.ok(marketCoupon);
        } else {
            return BaseRespVo.invalidData("显示优惠卷出错！");
        }
    }
    @RequestMapping("selectlist")
//    选择优惠卷
    public BaseRespVo<CouponVo> selectlist(Integer cartId, Integer grouponRulesId) {

        System.out.println("selectlist params cartId= " + cartId+",grouponRulesId="+grouponRulesId);

        CouponVo marketCoupon =new CouponVo();
        marketCoupon=popularizeService.selectlist(cartId,grouponRulesId);
        System.out.println("selectlist marketCoupon="+marketCoupon);
        if (marketCoupon != null) {
            return BaseRespVo.ok(marketCoupon);
        } else {
            return BaseRespVo.invalidData("选择优惠卷出错！");
        }
    }

}
